﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace zEngine
{
    public class zLight
    {
        private Vector3 _lightPosition = new Vector3(1, 1, 1);
        private Color _color = Color.White;
        private float _intensity = 3.0f;
        private Matrix? _lightViewProj = null;
        private Vector3 _direction = new Vector3(1, 1, 1);
        private GraphicsDevice _graphicsDevice;

        /// <summary>
        /// Initializes a new instance of the Light class.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        public zLight(GraphicsDevice graphicsDevice)
        {
            _graphicsDevice = graphicsDevice;
        }

        public void Update()
        {
            // Need to create a view target for CreateLookAt.
            // Could calculate from _direction, couldn't we?
            Vector3 lightTarget = _lightPosition - _direction;
            Matrix lightView = Matrix.CreateLookAt(Vector3.Zero, -_direction, Vector3.Up);
            Matrix lightProj = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, _graphicsDevice.Viewport.AspectRatio, 1.0f, 2000.0f);
            _lightViewProj = lightView * lightProj;
        }


        public GraphicsDevice GraphicsDevice
        {
            get { return _graphicsDevice; }
            set { _graphicsDevice = value; }
        }
        public Vector3 Direction
        {
            get { return _direction; }
            set { _direction = value; }
        }
        public Matrix LightViewProj
        {
            get
            {
                if (!_lightViewProj.HasValue)
                {
                    Update();
                }
                return _lightViewProj.Value;
            }
            set { _lightViewProj = value; }
        }
        public float Intensity
        {
            get { return _intensity; }
            set { _intensity = value; }
        }
        public Color Color
        {
            get { return _color; }
            set { _color = value; }
        }
        public Vector3 LightPosition
        {
            get { return _lightPosition; }
            set { _lightPosition = value; }
        }
    }
}
